package com.example.controller;

import cn.hutool.core.util.ObjectUtil;
import com.example.utils.Result;
import com.example.utils.RoleEnum;
import com.example.entity.Account;
import com.example.service.AdminService;
import com.example.service.StudentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
public class WebController {

    @Resource
    private AdminService adminService;

    @Resource
    private StudentService studentService;

    // 根路径请求，返回成功信息
    @GetMapping("/")
    public Result hello() {
        return Result.success();
    }

    // 登录请求处理
    @PostMapping("/login")
    public Result login(@RequestBody Account account) {
        Account dbAccount;
        if (RoleEnum.ADMIN.name().equals(account.getRole())) {
            dbAccount = adminService.login(account); // 管理员登录
        } else if (RoleEnum.TEACHER.name().equals(account.getRole())) {
            dbAccount = adminService.login(account); // 教师登录
        } else if (RoleEnum.STUDENT.name().equals(account.getRole())) {
            dbAccount = studentService.login(account); // 学生登录
        }else {
            return Result.error("角色错误"); // 角色错误
        }
        return Result.success(dbAccount); // 返回登录成功信息
    }

    // 注册请求处理
    @PostMapping("/register")
    public Result register(@RequestBody Account account) {
        if (ObjectUtil.isEmpty(account.getUsername()) || ObjectUtil.isEmpty(account.getPassword())) {
            return Result.error("账号或密码必填"); // 账号或密码为空
        }
        studentService.register(account); // 学生注册
        return Result.success(); // 返回注册成功信息
    }
}

